{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximum Enemy Forts That Can Be Captured"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: captureForts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最多可以摧毁的敌人城堡数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个长度为 <code>n</code>&nbsp;，下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>forts</code>&nbsp;，表示一些城堡。<code>forts[i]</code> 可以是&nbsp;<code>-1</code>&nbsp;，<code>0</code>&nbsp;或者&nbsp;<code>1</code>&nbsp;，其中：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>-1</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个位置 <strong>没有</strong>&nbsp;城堡。</li>\n",
    "\t<li><code>0</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个位置有一个 <strong>敌人</strong>&nbsp;的城堡。</li>\n",
    "\t<li><code>1</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个位置有一个你控制的城堡。</li>\n",
    "</ul>\n",
    "\n",
    "<p>现在，你需要决定，将你的军队从某个你控制的城堡位置&nbsp;<code>i</code>&nbsp;移动到一个空的位置&nbsp;<code>j</code>&nbsp;，满足：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= i, j &lt;= n - 1</code></li>\n",
    "\t<li>军队经过的位置 <strong>只有</strong>&nbsp;敌人的城堡。正式的，对于所有&nbsp;<code>min(i,j) &lt; k &lt; max(i,j)</code>&nbsp;的&nbsp;<code>k</code>&nbsp;，都满足&nbsp;<code>forts[k] == 0</code>&nbsp;。</li>\n",
    "</ul>\n",
    "\n",
    "<p>当军队移动时，所有途中经过的敌人城堡都会被 <strong>摧毁</strong> 。</p>\n",
    "\n",
    "<p>请你返回 <strong>最多</strong>&nbsp;可以摧毁的敌人城堡数目。如果 <strong>无法</strong>&nbsp;移动你的军队，或者没有你控制的城堡，请返回 <code>0</code>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>forts = [1,0,0,-1,0,0,0,0,1]\n",
    "<b>输出：</b>4\n",
    "<strong>解释：</strong>\n",
    "- 将军队从位置 0 移动到位置 3 ，摧毁 2 个敌人城堡，位置分别在 1 和 2 。\n",
    "- 将军队从位置 8 移动到位置 3 ，摧毁 4 个敌人城堡。\n",
    "4 是最多可以摧毁的敌人城堡数目，所以我们返回 4 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>forts = [0,0,1,-1]\n",
    "<b>输出：</b>0\n",
    "<b>解释：</b>由于无法摧毁敌人的城堡，所以返回 0 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= forts.length &lt;= 1000</code></li>\n",
    "\t<li><code>-1 &lt;= forts[i] &lt;= 1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximum-enemy-forts-that-can-be-captured](https://leetcode.cn/problems/maximum-enemy-forts-that-can-be-captured/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximum-enemy-forts-that-can-be-captured](https://leetcode.cn/problems/maximum-enemy-forts-that-can-be-captured/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,0,0,-1,0,0,0,0,1]', '[0,0,1,-1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        res = 0 \n",
    "        pre = -1\n",
    "        for i,v in enumerate(forts):\n",
    "            if v:\n",
    "                if pre >=0 and forts[pre] != v :\n",
    "                    res = max(res, i-pre-1)\n",
    "                pre = i\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "      rlt = 0\n",
    "      for idx, f in enumerate(forts):\n",
    "        left = 0\n",
    "        right = 0\n",
    "        if f != 1:\n",
    "          continue\n",
    "        for i in range(idx - 1, -1, -1):\n",
    "          x = forts[i]\n",
    "          if x == -1:\n",
    "            left = idx - i - 1\n",
    "            break\n",
    "          elif x == 1:\n",
    "            left = 0\n",
    "            break\n",
    "        for i in range(idx + 1, len(forts)):\n",
    "          x = forts[i]\n",
    "          if x == -1:\n",
    "            right = i - idx - 1\n",
    "            break\n",
    "          elif x == 1:\n",
    "            right = 0\n",
    "            break\n",
    "        rlt = max(rlt, left, right)\n",
    "      return rlt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        i = 0  # 标记上一个不为0的位置\n",
    "        n = len(forts)\n",
    "        while i < n and forts[i] == 0: i += 1   # 找到首个不为0的位置\n",
    "        res = 0\n",
    "        for j in range(i + 1, n):\n",
    "            if forts[j] != 0:\n",
    "                if forts[j] == -forts[i]: res = max(res, j - i - 1)    # fort[j]和fort[i]的值互为相反数，更新距离\n",
    "                i = j  # 只要fort[j]不为0，就更新i，即上一个不为0的位置发生了变化\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        cnt = 0\n",
    "        pre = -1\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i] != 0:\n",
    "                if pre >=0 and forts[i] != forts[pre]:\n",
    "                    cnt = max(cnt, i - pre - 1)\n",
    "                pre = i\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans = 0\n",
    "        pre_val = 0\n",
    "        pre_index = -1\n",
    "        for index, val in enumerate(forts):\n",
    "            if val in (1, -1):\n",
    "                if val == -pre_val:\n",
    "                    ans = max(ans, index - pre_index - 1)\n",
    "                pre_val = val\n",
    "                pre_index = index\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        last_idx = 0\n",
    "        last_flag = 0\n",
    "        tmp_max = 0\n",
    "\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i] != 0:\n",
    "                if last_flag + forts[i] == 0:\n",
    "                    tmp = i - last_idx - 1\n",
    "                    if tmp > tmp_max:\n",
    "                        tmp_max = tmp\n",
    "\n",
    "                last_flag = forts[i]\n",
    "                last_idx = i\n",
    "\n",
    "        return tmp_max\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        if len(forts) == 1:\n",
    "            return 0\n",
    "        left, right = 0, 1\n",
    "        maximum = 0\n",
    "        while right < len(forts):\n",
    "            if forts[left] in [1, -1]:\n",
    "                while right < len(forts) and forts[right] == 0:\n",
    "                    right += 1\n",
    "                if right < len(forts) and forts[right] != forts[left]:\n",
    "                    maximum = max(maximum, right - left - 1)\n",
    "                left, right = right, right + 1\n",
    "            else:\n",
    "                left, right = left + 1, right + 1\n",
    "        return maximum\n",
    "                    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        left, right = 2, -2\n",
    "        max_cas = 0\n",
    "        cout = 0\n",
    "        for i in forts:\n",
    "            if i != 0:\n",
    "                left = right\n",
    "                right = i\n",
    "                if right*left == -1 and cout > max_cas:\n",
    "                    max_cas = cout\n",
    "                cout = 0\n",
    "            else:\n",
    "                cout += 1\n",
    "        return max_cas\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre_i = 0, -1\n",
    "        for i, p in enumerate(forts):\n",
    "            if p == 1 or p == -1:\n",
    "                if pre_i >= 0 and p != forts[pre_i]:\n",
    "                    ans = max(ans, i - pre_i - 1)\n",
    "                pre_i = i\n",
    "        return ans\n",
    "\n",
    "        \n",
    "\n",
    "            \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        n = len(forts)\n",
    "        i  = ans =  0\n",
    "        while i < n:\n",
    "            j = i + 1\n",
    "            if forts[i]:\n",
    "                while j < n and forts[j] == 0:\n",
    "                    j += 1\n",
    "                    if j <n and forts[i] + forts[j] ==0:\n",
    "                        ans = max(ans, j -i -1)\n",
    "                    \n",
    "            i = j\n",
    "        return ans \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans,l = 0,0\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i]*forts[l]==-1 :\n",
    "                ans = max(ans,i-l-1)\n",
    "            if(forts[i]!=0):\n",
    "                l = i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        distance = []\n",
    "        start = 0\n",
    "        end = 0\n",
    "        target = 1\n",
    "        for i in range(len(forts)):\n",
    "            if target:\n",
    "                if forts[i]==-1 or forts[i]==1:\n",
    "                    start=i\n",
    "                    end=i\n",
    "                    target=0\n",
    "            else:\n",
    "                if forts[i]==-1 or forts[i]==1:\n",
    "                    end = i\n",
    "                    if forts[start]+forts[end]==0:\n",
    "                        distance.append(end-start-1)\n",
    "                    start=i\n",
    "                else:\n",
    "                    end = i\n",
    "\n",
    "        return max(distance) if len(distance)>0 else 0\n",
    "            \n",
    "            \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans = 0\n",
    "        pre = -1\n",
    "        for i ,fort in enumerate(forts):\n",
    "            if fort == -1 or fort ==1:\n",
    "                if pre >=0 and fort !=forts[pre]:\n",
    "                    ans = max(ans,i - pre -1)\n",
    "                pre = i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans = 0\n",
    "        n = len(forts)\n",
    "        for i in range(n):\n",
    "            if forts[i] == 1:\n",
    "                tmp = 0\n",
    "                f = 0\n",
    "                for j in range(i + 1,n):\n",
    "                    if forts[j] == 0:\n",
    "                        tmp += 1\n",
    "                    elif forts[j] == -1:\n",
    "                        f = 1\n",
    "                        break\n",
    "                    else:\n",
    "                        break\n",
    "                if f:\n",
    "                    ans = max(ans,tmp)\n",
    "                tmp = 0\n",
    "                f = 0\n",
    "                for j in range(i - 1,-1,-1):\n",
    "                    if forts[j] == 0:\n",
    "                        tmp += 1\n",
    "                    elif forts[j] == -1:\n",
    "                        f = 1\n",
    "                        break\n",
    "                    else:\n",
    "                        break\n",
    "                if f:\n",
    "                    ans = max(ans,tmp)\n",
    "        return ans\n",
    "                \n",
    "                        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre = 0, -1\n",
    "        for i, fort in enumerate(forts):\n",
    "            if fort == -1 or fort == 1:\n",
    "                if pre >= 0 and fort != forts[pre]:\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre = 0, -1\n",
    "        for i, fort in enumerate(forts):\n",
    "            if fort == -1 or fort == 1:\n",
    "                if pre >= 0 and fort != forts[pre]:\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        left = None\n",
    "        right = None\n",
    "        if len(set(forts)) != 3:\n",
    "            return 0\n",
    "        ans = 0\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i] == 1 or forts[i] == -1:\n",
    "                if left is None:\n",
    "                    left = i\n",
    "                elif right is None:\n",
    "                    right = i\n",
    "                else:\n",
    "                    left = right\n",
    "                    right = i\n",
    "                if left is not None and right is not None and forts[left] != forts[right]:\n",
    "                    if right - left > ans:\n",
    "                        ans = right - left\n",
    "        return ans - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        i,j=0,0\n",
    "        n=len(forts)\n",
    "        ans=0\n",
    "        while i<n:\n",
    "            if forts[i]!=0:\n",
    "                if forts[j]==-1*forts[i]:\n",
    "                    ans=max(ans,i-j-1)\n",
    "                    j=i\n",
    "                else:\n",
    "                    j=i\n",
    "            \n",
    "            i+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        dis=0\n",
    "        n=len(forts)\n",
    "        for i in range(n):\n",
    "            if forts[i]==-1:\n",
    "                x,y=i,i\n",
    "                ans1,ans2=0,0\n",
    "                while forts[x]!=1:\n",
    "                    x-=1\n",
    "                    if x==-1 or forts[x]==-1:\n",
    "                        ans1=0\n",
    "                        break\n",
    "                    ans1+=1\n",
    "                while forts[y]!=1:\n",
    "                    y+=1\n",
    "                    if y==n or forts[y]==-1:\n",
    "                        ans2=0\n",
    "                        break\n",
    "                    ans2+=1\n",
    "                dis=max(dis,ans1-1,ans2-1)\n",
    "        return dis\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        dis=0\n",
    "        n=len(forts)\n",
    "        for i in range(n):\n",
    "            if forts[i]==-1:\n",
    "                x,y=i,i\n",
    "                ans1,ans2=0,0\n",
    "                while forts[x]!=1:\n",
    "                    x-=1\n",
    "                    if x==-1 or forts[x]==-1:\n",
    "                        ans1=0\n",
    "                        break\n",
    "                    ans1+=1\n",
    "                while forts[y]!=1:\n",
    "                    y+=1\n",
    "                    if y==n or forts[y]==-1:\n",
    "                        ans2=0\n",
    "                        break\n",
    "                    ans2+=1\n",
    "                dis=max(dis,ans1-1,ans2-1)\n",
    "        return dis\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def captureForts(self, forts):\n",
    "        p = 0\n",
    "        q = 0\n",
    "        res = 0\n",
    "        while q < len(forts):\n",
    "            q = p + 1\n",
    "            if forts[p]:\n",
    "                while q < len(forts) and forts[q] == 0:\n",
    "                    q += 1\n",
    "                if q < len(forts) and forts[p] + forts[q] == 0:\n",
    "                    res = max(res,q - p - 1)\n",
    "            p += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans=0\n",
    "        n=len(forts)\n",
    "        f=-1\n",
    "        for i in range(n):\n",
    "            if forts[i]!=0:\n",
    "                if f!=-1 and forts[f]!=forts[i]:\n",
    "                    ans=max(ans,i-f-1)\n",
    "                f=i\n",
    "\n",
    "        return ans\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        # 寻找1和-1之间最长的连续0\n",
    "        \n",
    "        # 寻找第一个非零\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i]:\n",
    "                break\n",
    "        last = forts[i]\n",
    "        cnt = ans = 0\n",
    "        for f in forts[i+1:]:\n",
    "            if f == 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                if f != last:\n",
    "                    last = f\n",
    "                    if cnt > ans:\n",
    "                        ans = cnt\n",
    "                cnt = 0\n",
    "                \n",
    "        return ans\n",
    "                \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans =0\n",
    "        pre =-1\n",
    "        for i,x in enumerate(forts):\n",
    "            if x:\n",
    "                if pre>=0 and x!= forts[pre]:\n",
    "                    ans = max(ans,i-pre-1)\n",
    "                pre =i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre = 0, -1\n",
    "        for i, fort in enumerate(forts):\n",
    "            if fort == -1 or fort == 1:\n",
    "                if pre >= 0 and fort != forts[pre]:\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        left = 0\n",
    "        right = left + 1\n",
    "        ans = 0\n",
    "        while right < len(forts):\n",
    "            if forts[left] == 0:\n",
    "                left += 1\n",
    "                right = left + 1\n",
    "                continue\n",
    "            if forts[left]+forts[right] == 1 or forts[left]+forts[right] == -1:\n",
    "                right += 1\n",
    "            elif forts[left]+forts[right] == 0:\n",
    "                ans = max(ans,right-left-1)\n",
    "                left = right\n",
    "                right += 1\n",
    "            else:\n",
    "                left = right\n",
    "                right = left+1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        n = len(forts)\n",
    "        i = ans = 0\n",
    "        while i < n:\n",
    "            j = i + 1\n",
    "            if forts[i]:\n",
    "                while j < n and forts[j] == 0:\n",
    "                    j += 1\n",
    "                if j < n and forts[i] + forts[j] == 0:\n",
    "                    ans = max(ans, j - i - 1)\n",
    "            i = j\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans=0\n",
    "        pre=-1\n",
    "        for i,x in enumerate(forts):\n",
    "            if x:\n",
    "                if pre>=0 and x!=forts[pre]:\n",
    "                    ans=max(ans,i-pre-1)\n",
    "                pre=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans = 0\n",
    "        pre = -1  # 表示不存在\n",
    "        for i, x in enumerate(forts):\n",
    "            if x:  # 1 或 -1\n",
    "                if pre >= 0 and x != forts[pre]:  # 一个是 1，另一个是 -1\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        dis=0\n",
    "        n=len(forts)\n",
    "        for i in range(n):\n",
    "            if forts[i]==-1:\n",
    "                x,y=i,i\n",
    "                ans1,ans2=0,0\n",
    "                while forts[x]!=1:\n",
    "                    x-=1\n",
    "                    if x==-1 or forts[x]==-1:\n",
    "                        ans1=0\n",
    "                        break\n",
    "                    ans1+=1\n",
    "                while forts[y]!=1:\n",
    "                    y+=1\n",
    "                    if y==n or forts[y]==-1:\n",
    "                        ans2=0\n",
    "                        break\n",
    "                    ans2+=1\n",
    "                dis=max(dis,ans1-1,ans2-1)\n",
    "        return dis\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        def static(fort):\n",
    "            res, start, start_reverse = 0, -1, -1\n",
    "            for i, x in enumerate(fort):\n",
    "                if x == 1:\n",
    "                    if start_reverse > -1:\n",
    "                        res = max(res, i-start_reverse-1)\n",
    "                        start_reverse = -1\n",
    "                    start = i\n",
    "                elif x == -1:\n",
    "                    if start > -1:\n",
    "                        res = max(res, i-start-1)\n",
    "                        start = -1\n",
    "                    start_reverse = i\n",
    "            return res\n",
    "        return static(forts)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(forts)):\n",
    "            for j in range(i+1,len(forts)):\n",
    "                if (((forts[i] == 1 and forts[j] == -1) or (forts[i] == -1 and forts[j] == 1)) and forts[i+1:j] == [0]*(j-i-1)) and (j -i-1)> ans:\n",
    "                    ans = (j -i-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        i, j = 0, 0\n",
    "        ans = 0\n",
    "        while i < len(forts) and forts[i] == 0:\n",
    "            i += 1\n",
    "        while j < len(forts):\n",
    "            if forts[j] != 0:\n",
    "                if forts[i] != forts[j]:\n",
    "                    ans = max(ans, j - i - 1)\n",
    "                i = j\n",
    "            j += 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        # dis=0\n",
    "        # n=len(forts)\n",
    "        # for i in range(n):\n",
    "        #     if forts[i]==-1:\n",
    "        #         x,y=i,i\n",
    "        #         ans1,ans2=0,0\n",
    "        #         while forts[x]!=1:\n",
    "        #             x-=1\n",
    "        #             if x==-1 or forts[x]==-1:\n",
    "        #                 ans1=0\n",
    "        #                 break\n",
    "        #             ans1+=1\n",
    "        #         while forts[y]!=1:\n",
    "        #             y+=1\n",
    "        #             if y==n or forts[y]==-1:\n",
    "        #                 ans2=0\n",
    "        #                 break\n",
    "        #             ans2+=1\n",
    "        #         dis=max(dis,ans1-1,ans2-1)\n",
    "        # return dis\n",
    "\n",
    "        fort_num = 0\n",
    "        pre = -1\n",
    "        for i, v in enumerate(forts):\n",
    "            if v:\n",
    "                if pre >= 0 and forts[pre] != v:\n",
    "                    fort_num = max(fort_num, abs(pre - i) - 1)\n",
    "                pre = i\n",
    "        return fort_num\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre = 0, -1\n",
    "        for i, fort in enumerate(forts):\n",
    "            if fort == -1 or fort == 1:\n",
    "                if pre >= 0 and fort !=forts[pre]:\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i \n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre = 0, -1\n",
    "        for i, fort in enumerate(forts):\n",
    "            if fort == -1 or fort == 1:\n",
    "                if pre >= 0 and fort != forts[pre]:\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        destory = 0\n",
    "        pre = -1\n",
    "\n",
    "        for fort in range(0,len(forts)):\n",
    "            if forts[fort] == -1 or forts[fort] == 1:\n",
    "                if pre != -1 and forts[fort] != forts[pre]:\n",
    "                    destory = max(destory, fort - pre - 1)\n",
    "                pre = fort\n",
    "        return destory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        n = len(forts)\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            if forts[i] != 1: continue\n",
    "            j = i + 1\n",
    "            while j < n and forts[j] == 0:\n",
    "                j += 1\n",
    "            if j < n and forts[j] == -1:\n",
    "                ans = max(ans, j - i - 1)\n",
    "            j = i - 1\n",
    "            while j >= 0 and forts[j] == 0:\n",
    "                j -= 1\n",
    "            if j >= 0 and forts[j] == -1:\n",
    "                ans = max(ans, i - j - 1)\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        left = 0\n",
    "        right = left + 1\n",
    "        ans = 0\n",
    "        while right < len(forts):\n",
    "            if forts[left] == 0:\n",
    "                left += 1\n",
    "                right = left + 1\n",
    "                continue\n",
    "            if forts[left]+forts[right] == 1 or forts[left]+forts[right] == -1:\n",
    "                right += 1\n",
    "            elif forts[left]+forts[right] == 0:\n",
    "                ans = max(ans,right-left-1)\n",
    "                left = right\n",
    "                right += 1\n",
    "            else:\n",
    "                left = right\n",
    "                right = left+1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "      ans, pre = 0, -1\n",
    "      for i, fort in enumerate(forts):\n",
    "          if fort == -1 or fort == 1:\n",
    "              if pre >= 0 and fort != forts[pre]:\n",
    "                  ans = max(ans, i - pre - 1)\n",
    "              pre = i\n",
    "      return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        i, j = 0, 0\n",
    "        ans = 0\n",
    "        while i < len(forts) and forts[i] == 0:\n",
    "            i += 1\n",
    "        while j < len(forts):\n",
    "            if forts[j] == 0:\n",
    "                j += 1\n",
    "            else:\n",
    "                if forts[i] != forts[j]:\n",
    "                    ans = max(ans, j - i - 1)\n",
    "                i = j\n",
    "                j += 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre = 0, -1\n",
    "        for i, fort in enumerate(forts):\n",
    "            if fort == -1 or fort == 1:\n",
    "                if pre >= 0 and fort != forts[pre]:\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre = 0, -1\n",
    "        for i, fort in enumerate(forts):\n",
    "            if fort == -1 or fort == 1:\n",
    "                if pre >= 0 and fort != forts[pre]:\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        i = 0\n",
    "        j = 0\n",
    "        l = len(forts)\n",
    "        max_l = 0\n",
    "\n",
    "        while i < l:\n",
    "            if forts[i] == -1 or forts[i] == 0:\n",
    "                i += 1\n",
    "                j = i\n",
    "            else:\n",
    "                tmp = 0\n",
    "                j += 1\n",
    "                while j < l:\n",
    "                    if forts[j] == 1:\n",
    "                        tmp = 0\n",
    "                        break\n",
    "                    if j == l - 1 and forts[j] == 0:\n",
    "                        tmp = 0\n",
    "                        break\n",
    "                    if forts[j] == -1:\n",
    "                        if max_l < tmp:\n",
    "                            max_l = tmp\n",
    "                        tmp = 0\n",
    "                        break\n",
    "                    if forts[j] == 0:\n",
    "                        j += 1\n",
    "                        tmp += 1\n",
    "                j = i - 1\n",
    "\n",
    "                while j > -1:\n",
    "                    if forts[j] == 1:\n",
    "                        tmp = 0\n",
    "                        break\n",
    "                    if j == 0 and forts[j] == 0:\n",
    "                        tmp = 0\n",
    "                        break\n",
    "                    if forts[j] == -1:\n",
    "                        if max_l < tmp:\n",
    "                            max_l = tmp\n",
    "                        tmp = 0\n",
    "                        break\n",
    "                    if forts[j] == 0:\n",
    "                        j -= 1\n",
    "                        tmp += 1\n",
    "                \n",
    "                i += 1\n",
    "                j = i\n",
    "\n",
    "        return max_l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        # 寻找-1和1中最多连续0的个数   面向连续0编程\n",
    "        n = len(forts)\n",
    "        begin = False\n",
    "        res = 0\n",
    "        cur = 0\n",
    "        left = 0\n",
    "        for i in range(1, n):\n",
    "            if not begin:\n",
    "                if forts[i] == 0:\n",
    "                    begin = True\n",
    "                    left = i - 1\n",
    "                    cur += 1\n",
    "            else:\n",
    "                if forts[i] == 0:\n",
    "                    cur += 1\n",
    "                else:\n",
    "                    if forts[left] == 1 and forts[i] == -1:\n",
    "                        res = max(res, cur)\n",
    "                    elif forts[left] == -1 and forts[i] == 1:\n",
    "                        res = max(res, cur)\n",
    "                    cur = 0\n",
    "                    begin = False\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans = 0\n",
    "        pre = -1  # 表示不存在\n",
    "        for i, x in enumerate(forts):\n",
    "            if x:  # 1 或 -1\n",
    "                if pre >= 0 and x != forts[pre]:  # 一个是 1，另一个是 -1\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        num = 0\n",
    "        left = 0\n",
    "        max1 = 0\n",
    "        while left >=0 and left < len(forts) and (forts[left]!=1 and forts[left]!=-1):\n",
    "            left+=1\n",
    "        for i in range(left+1,len(forts)):\n",
    "            if forts[i]!=0 and forts[left]!=0 and forts[i]!=forts[left]:\n",
    "                max1 = max(max1,i-left-1)\n",
    "                left = i\n",
    "            elif forts[i]!=0 and forts[left]!=0 and forts[i]==forts[left]:\n",
    "                \n",
    "                left = i\n",
    "        return max1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        res = 0\n",
    "        LEN = len(forts)\n",
    "        for i in range(LEN - 2):\n",
    "            if forts[i] == 1 and forts[i + 1] == 0:\n",
    "                curr = 0\n",
    "                while i < LEN - 1 and forts[i + 1] == 0:\n",
    "                    curr += 1\n",
    "                    i += 1\n",
    "                if i != LEN - 1 and forts[i + 1] == -1:\n",
    "                    res = max(res, curr)\n",
    "\n",
    "\n",
    "        for j in range(LEN - 1, 0, -1):\n",
    "            if forts[j] == 1 and forts[j - 1] == 0:\n",
    "                curr = 0\n",
    "                while j > 0 and forts[j - 1] == 0:\n",
    "                    curr += 1\n",
    "                    j -= 1\n",
    "                if j != 0 and forts[j - 1] == -1:\n",
    "                    res = max(res, curr)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        # dis=0\n",
    "        # n=len(forts)\n",
    "        # for i in range(n):\n",
    "        #     if forts[i]==-1:\n",
    "        #         x,y=i,i\n",
    "        #         ans1,ans2=0,0\n",
    "        #         while forts[x]!=1:\n",
    "        #             x-=1\n",
    "        #             if x==-1 or forts[x]==-1:\n",
    "        #                 ans1=0\n",
    "        #                 break\n",
    "        #             ans1+=1\n",
    "        #         while forts[y]!=1:\n",
    "        #             y+=1\n",
    "        #             if y==n or forts[y]==-1:\n",
    "        #                 ans2=0\n",
    "        #                 break\n",
    "        #             ans2+=1\n",
    "        #         dis=max(dis,ans1-1,ans2-1)\n",
    "        # return dis\n",
    "\n",
    "        fort_num = 0\n",
    "        pre = -1\n",
    "        for i, v in enumerate(forts):\n",
    "            if v:\n",
    "                if pre >= 0 and forts[pre] != v:\n",
    "                    fort_num = max(fort_num, abs(pre - i) - 1)\n",
    "                pre = i\n",
    "        return fort_num\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans=0\n",
    "        for i,x in enumerate(forts):\n",
    "            if x!=1:\n",
    "                continue\n",
    "            j=i-1\n",
    "            while j>=0 and forts[j]==0:\n",
    "                j-=1\n",
    "            if j>=0 and forts[j]==-1:\n",
    "                ans=max(ans,i-j-1)\n",
    "            j=i+1\n",
    "            while j<len(forts) and forts[j]==0:\n",
    "                j+=1\n",
    "            if j<len(forts) and forts[j]==-1:\n",
    "                ans=max(ans,j-i-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        n = len(forts)\n",
    "        left = 0\n",
    "        res = 0\n",
    "        while left<n and forts[left]==0:\n",
    "            left+=1\n",
    "        print(left)\n",
    "        for right in range(left+1,n):\n",
    "            if forts[right]!=0:\n",
    "                if forts[right]==-forts[left]:\n",
    "                    res = max(res,right-left-1)\n",
    "                    # print(right-left-1)\n",
    "                left = right\n",
    "        # print(left)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(forts)):\n",
    "            for j in range(i+1,len(forts)):\n",
    "                if(((forts[i]==1 and forts[j]==-1)or(forts[i]==-1 and forts[j]==1))and forts[i+1:j]==[0]*(j-i-1))and(j-i-1)>ans:\n",
    "                    ans=(j-i-1)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        i, j = 0, 0\n",
    "        ans = 0\n",
    "        while i < len(forts) and forts[i] == 0:\n",
    "            i += 1\n",
    "        while j < len(forts):\n",
    "            if forts[j] != 0:\n",
    "                if forts[i] != forts[j]:\n",
    "                    ans = max(ans, j - i - 1)\n",
    "                i = j\n",
    "            j += 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        left = 0\n",
    "        right = left + 1\n",
    "        ans = 0\n",
    "        while right < len(forts):\n",
    "            if forts[left] == 0:\n",
    "                left += 1\n",
    "                right = left + 1\n",
    "                continue\n",
    "            if forts[left]+forts[right] == 1 or forts[left]+forts[right] == -1:\n",
    "                right += 1\n",
    "            elif forts[left]+forts[right] == 0:\n",
    "                ans = max(ans,right-left-1)\n",
    "                left = right\n",
    "                right += 1\n",
    "            else:\n",
    "                left = right\n",
    "                right = left+1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts) -> int:\n",
    "        fort_num = 0\n",
    "        pre = -1\n",
    "        for i, v in enumerate(forts):\n",
    "            if v:\n",
    "                if pre >= 0 and forts[pre] != v:\n",
    "                    fort_num = max(fort_num, abs(pre - i) - 1)\n",
    "                pre = i\n",
    "        return fort_num\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        res,pre=0,-1\n",
    "        for i,f in enumerate(forts):\n",
    "            if f==-1 or f==1:\n",
    "                if pre>=0 and f!=forts[pre]:\n",
    "                    res=max(res,i-pre-1)\n",
    "                pre=i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        # 寻找1和-1之间最长的连续0\n",
    "        \n",
    "        # 寻找第一个非零\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i]:\n",
    "                break\n",
    "        last = forts[i]\n",
    "        cnt = ans = 0\n",
    "        for f in forts[i+1:]:\n",
    "            if f == 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                if f != last and cnt > ans:\n",
    "                    ans = cnt\n",
    "                cnt = 0\n",
    "                last = f\n",
    "        return ans\n",
    "                \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        last=-1\n",
    "        lastv=0\n",
    "        result=0\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i]!=0:\n",
    "                if lastv==-forts[i] and i-last-1>result:\n",
    "                    result=i-last-1\n",
    "                last=i\n",
    "                lastv=forts[i]\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans, pre = 0, -1\n",
    "        for i, fort in enumerate(forts):\n",
    "            if fort == -1 or fort == 1:   # 当前必须是1/-1\n",
    "                if pre >= 0 and fort != forts[pre]:   # 前一个是1/-1且与现在的不同\n",
    "                    ans = max(ans, i - pre - 1)\n",
    "                pre = i\n",
    "        return ans\n",
    "\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        n = len(forts)\n",
    "        pre = -1\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            if forts[i]:\n",
    "                if pre >= 0 and forts[i] != forts[pre]:\n",
    "                    ans = max(ans, i-pre-1)\n",
    "                pre = i\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        n = len(forts)\n",
    "        i  = ans =  0\n",
    "        while i < n:\n",
    "            j = i + 1\n",
    "            if forts[i]:\n",
    "                while j < n and forts[j] == 0:\n",
    "                    j += 1\n",
    "                if j < n and forts[i] + forts[j] ==0:\n",
    "                    ans = max(ans, j -i -1)\n",
    "                    \n",
    "            i = j\n",
    "        return ans \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        n = len(forts)\n",
    "        res=0\n",
    "        i=0\n",
    "        while i<n:\n",
    "            j=i+1\n",
    "            while j<n and forts[j]==0:\n",
    "                j+=1\n",
    "\n",
    "            if j<n and forts[i]+forts[j]==0:\n",
    "                res=max(res,j-i-1)\n",
    "            i=j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans,pre = 0,-1\n",
    "        for i,fort in enumerate(forts):\n",
    "            if fort == 1 or fort == -1:\n",
    "                if pre >= 0 and fort != forts[pre]:\n",
    "                    ans = max(ans,i - pre - 1)\n",
    "                pre = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        ans,pre=0,-1\n",
    "        for i,x in enumerate(forts):\n",
    "            if x:\n",
    "                if pre>=0 and x!=forts[pre]:\n",
    "                    ans=max(ans,i-pre-1)\n",
    "                pre=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "\n",
    "        dp = [None, None]\n",
    "        result = 0\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i]!=0:\n",
    "                if (forts[i]==1):\n",
    "                    dp[0] = i\n",
    "                    if (dp[1] is not None):\n",
    "                        result = max(i-dp[1]-1, result)\n",
    "                        dp[1] = None\n",
    "                else:\n",
    "                    dp[1] = i\n",
    "                    if (dp[0] is not None):\n",
    "                        result = max(i-dp[0]-1, result)\n",
    "                        dp[0] = None\n",
    "            # print(dp)\n",
    "            # print(result)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        if -1 not in forts or 1 not in forts:\n",
    "            return 0\n",
    "\n",
    "\n",
    "        res = []\n",
    "        indices = [index for index, element in enumerate(forts) if element == -1]\n",
    "        \n",
    "        for indice in indices:\n",
    "            n = 1\n",
    "            while indice >= n:\n",
    "                if forts[indice-n] == -1:\n",
    "                    break                \n",
    "                elif forts[indice-n] == 1:\n",
    "                    res.append(forts[indice-n:indice].count(0))\n",
    "                    break\n",
    "                \n",
    "\n",
    "                n += 1\n",
    "\n",
    "      \n",
    "        for indice in indices:\n",
    "            n = 1\n",
    "            while n<(len(forts)-indice):\n",
    "                if forts[indice+n] ==-1:\n",
    "                    break                   \n",
    "                if forts[indice+n] ==1:\n",
    "                    res.append(forts[indice:indice+n].count(0))\n",
    "                    break        \n",
    "                n += 1\n",
    "        \n",
    "        if len(res) != 0:\n",
    "            return max(res)\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        res = 0\n",
    "        pre = -1\n",
    "        for i,val in enumerate(forts):\n",
    "            if val == 1 or val == -1:\n",
    "                if pre >= 0 and val != forts[pre]:\n",
    "                    res = max(res,i-pre-1)\n",
    "                pre = i\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        n = len(forts)\n",
    "        ans1 = []\n",
    "        ans2 = []\n",
    "        if n == 1 or n == 2:\n",
    "            return 0\n",
    "        else:\n",
    "            for i in range(n):\n",
    "                if forts[i] == 1:\n",
    "                   ans1.append(i+1)\n",
    "                if forts[i] == -1:\n",
    "                   ans1.append(-i-1)\n",
    "            for j in range(len(ans1)):\n",
    "                if j == 0 and ans1[1]*ans1[0] < 0:\n",
    "                   ans2.append(abs(ans1[1])-abs(ans1[0])-1)\n",
    "                elif ans1[j-1]*ans1[j] < 0 :\n",
    "                   ans2.append(abs(ans1[j])-abs(ans1[j-1])-1)\n",
    "            result = max(ans2, default=0)\n",
    "            return result\n",
    "\n",
    "                \n",
    "\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "        pre = None\n",
    "        maxCount = 0\n",
    "\n",
    "        for i, n in enumerate(forts):\n",
    "            if n == 1:\n",
    "                if pre is not None and pre[0] == -1:\n",
    "                    maxCount = max(maxCount, i - pre[1] - 1)\n",
    "                pre = (1, i)\n",
    "\n",
    "            elif n == -1:\n",
    "                if pre is not None and pre[0] == 1:\n",
    "                    maxCount = max(maxCount, i - pre[1] - 1)\n",
    "                pre = (-1, i)\n",
    "            else:\n",
    "                continue\n",
    "        return maxCount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def captureForts(self, forts: List[int]) -> int:\n",
    "\n",
    "        ans = 0\n",
    "        empty = []\n",
    "        zeros = []\n",
    "        ones = []\n",
    "        for i in range(len(forts)):\n",
    "            if forts[i] == -1:\n",
    "                empty.append(i)\n",
    "            elif forts[i] == 0:\n",
    "                zeros.append(i)\n",
    "            else:\n",
    "                ones.append(i)\n",
    "        if not zeros or not empty:\n",
    "            return 0\n",
    "        for i in ones:\n",
    "            j = i+1\n",
    "            while j<len(forts) and forts[j]==0:\n",
    "                j += 1\n",
    "            if j < len(forts) and forts[j] == -1:\n",
    "                ans = max(ans, j-i-1)\n",
    "            j = i-1\n",
    "            while j>=0 and forts[j] == 0:\n",
    "                j -= 1\n",
    "            if j >=0 and forts[j] == -1:\n",
    "                ans = max(ans, i-j-1)\n",
    "        return ans\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
